#include "CH395.h"
uint8_t  s1_send_buffer_free = 1;
uint8_t  s1_tcp_connected    = 0;

RECV_BUFF  s1_rcv_buffer = {.length=0,.rcvflag=0};
RECV_BUFF  s0_rcv_buffer = {.length=0,.rcvflag=0};

//-------------------------------------------------------------------------------------
// CH395_init
// 初始化ch395
//-------------------------------------------------------------------------------------
uint8_t  CH395_if_config(pifconfig config)
{  
    uint8_t i = 0;
    uint8_t s = 0;
    
    ch395_set_ip_addr  (config->local_ip    );
    ch395_set_gwip_addr(config->local_getway);
    ch395_set_mask_addr(config->local_mask  );  
    UTIL_IdleUs(200000);                       //延时200ms 

    ch395_init_ch395();
    while(1)
    {
       UTIL_IdleUs(200000);
       s = ch395_get_cmd_status();                                     
       if(s !=CH395_ERR_BUSY)
         break;                                 
       if(i++ > 200)
         return CH395_ERR_UNKNOW;                        
    }   
    return s;             
}
//-------------------------------------------------------------------------------------
// CH395_socket_init(uint8_t sn, pifconfig config)
// 初始化socket
//-------------------------------------------------------------------------------------
void CH395_socket_init(uint8_t sn, pifconfig config)
{
    ch395_set_ip_addr_sn   (sn,config->remote_ip);                 //设置目的ip
    ch395_set_proto_type_sn(sn,config->prototype);                 //设置协议类型
    ch395_set_des_port_sn  (sn,config->remote_port);               //设置目的端口号
    ch395_set_sour_port_sn (sn,config->local_port);                //设置本地端口号
    UTIL_IdleUs(200000);                                           //延时200ms 
}

//-------------------------------------------------------------------------------------
// CH395_sn_open()
// 打开socket
//-------------------------------------------------------------------------------------
uint8_t CH395_socket_open(uint8_t sn)
{
    uint8_t i = 0;
    uint8_t s = 0;

    ch395_open_socket_sn(sn);                                      //打开socket n
    while(1)
    {
        UTIL_IdleUs(100000);                                                
        s = ch395_get_cmd_status();                                    
        if(s !=CH395_ERR_BUSY)
          break;                                 
        if(i++ > 200)
          return CH395_ERR_UNKNOW;                        
    }
    return s;   
}

//-------------------------------------------------------------------------------------
// CH395_socket_close()
// 关闭socket
//-------------------------------------------------------------------------------------
uint8_t CH395_socket_close(uint8_t sn)
{
    uint8_t i = 0;
    uint8_t s = 0;

    ch395_close_socket_sn(sn);                                      //关闭socket n
    while(1)
    {
        UTIL_IdleUs(100000);                                                
        s = ch395_get_cmd_status();                                    
        if(s !=CH395_ERR_BUSY)
          break;                                 
        if(i++ > 200)
          return CH395_ERR_UNKNOW;                        
    }
    return s;   
}

//-------------------------------------------------------------------------------------
// CH395_tcp_connect()
// tcp连接
//-------------------------------------------------------------------------------------
uint8_t CH395_tcp_connect(uint8_t sn)
{
    uint8_t i = 0;
    uint8_t s = 0;

    ch395_tcp_connect_sn(sn);
    while(1)
    {
        UTIL_IdleUs(100000);                                               
        s = ch395_get_cmd_status();                                     
        if(s !=CH395_ERR_BUSY)
          break;                                
        if(i++ > 200)
          return CH395_ERR_UNKNOW;                       
    }
    return s;
}
//-------------------------------------------------------------------------------------
// CH395_send_sn()
// tcp发送数据
//-------------------------------------------------------------------------------------
bool CH395_send_sn(uint8_t sn, uint16_t len, uint8_t* pbuf)
{ 
   bool ret=false;
   ret = ch395_write_send_buf_sn(sn,len,pbuf);
   s1_send_buffer_free = 0;
   return ret;
}
//-------------------------------------------------------------------------------------
// CH395_Socket1Interrupt()
// socket1中断处理
//-------------------------------------------------------------------------------------
static void CH395_Socket1Interrupt()
{
   uint8_t  sock_int_socket;

   sock_int_socket = ch395_get_int_status_sn(1);      
   if(sock_int_socket & SINT_STAT_SENBUF_FREE)                    //发送缓冲区空，可以继续向发送缓冲区写数据             
   {
      s1_send_buffer_free = 1;
   }

   if(sock_int_socket & SINT_STAT_RECV)                          //接收到数据           
   {
       
      s1_rcv_buffer.length = ch395_get_recv_len_sn(1); 
      s1_rcv_buffer.rcvflag= 1;
      ch395_get_recv_buf_sn(1,s1_rcv_buffer.length,s1_rcv_buffer.data);    
   }

   if(sock_int_socket & SINT_STAT_CONNECT)                       //tcp 连接成功                    
   { 
	   s1_tcp_connected=1;
	   ch395_set_keep_live_sn(1,1);								                  
	   ch395_set_ttl(1,40); 								                  
   }
   if(sock_int_socket & SINT_STAT_DISCONNECT)                   //tcp 断开连接     
   {
      printf("socket1 tcp disconnect\r\n");
      s1_tcp_connected=0;
	   CH395_socket_open(1);                                             
      CH395_tcp_connect(1);                                         
   }
   if(sock_int_socket & SINT_STAT_TIM_OUT)                      //超时  
   {
      printf("socket1 tcp timeout\r\n");
  	   s1_tcp_connected=0;
      CH395_socket_close(1);
	   CH395_socket_open(1);                                       
      CH395_tcp_connect(1);                                        
   }
}
//-------------------------------------------------------------------------------------
// CH395_Socket0Interrupt()
// socket0中断处理
//-------------------------------------------------------------------------------------
static void CH395_Socket0Interrupt()
{
   uint8_t  sock_int_socket;

   sock_int_socket = ch395_get_int_status_sn(0);      
   //printf("socket0 int stat:0x%02x\r\n",sock_int_socket);
   if(sock_int_socket & SINT_STAT_RECV)                          //接收到数据           
   {
      s0_rcv_buffer.length = ch395_get_recv_len_sn(0); 
      s0_rcv_buffer.rcvflag= 1;
      ch395_get_recv_buf_sn(0,s0_rcv_buffer.length,s0_rcv_buffer.data);    
   }
}

//-------------------------------------------------------------------------------------
// CH395_GlobalInterrupt()
// 中断处理
//-------------------------------------------------------------------------------------
uint16_t CH395_GlobalInterrupt(void)
{
    uint16_t  init_status;
    
    init_status = ch395_get_glob_int_status_all();
    if(init_status & GINT_STAT_UNREACH)                              
    {
      //printf("interrupt: unreach\r\n");
    }
    if(init_status & GINT_STAT_IP_CONFLI)                           
    {
       //printf("interrupt: ip conflit OR tcp server close connect\r\n");
    }
    if(init_status & GINT_STAT_PHY_CHANGE)                          
    {
       //printf("interrup:  phy changed\r\n");
    }
    if(init_status & GINT_STAT_SOCK0)
    {
        //printf("sock0 int\r\n");
        CH395_Socket0Interrupt();                                     
    }
    if(init_status & GINT_STAT_SOCK1)                                
    {
        //printf("sock1 int\r\n");
        CH395_Socket1Interrupt();                                  
    }

    return init_status;
}

